import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:t3_map/address_select.dart';
import 'package:t3_map/configs/poi_search_base_config.dart';
import 'package:t3_map/data_report/address_log.dart';
import 'package:t3_map/poi_search/view_model/poi_search_bar_view_model.dart';
import 'package:t3_map/poi_search/widget/search_bar/input_multi_board.dart';
import 'package:t3_map/poi_search/widget/search_bar/input_single_board.dart';
import 'package:uikit/uikit.dart';

import '../../model/const.dart';
import '../../view_model/poi_search_cell_view_model.dart';
import 'input_item_widget.dart';

class SearchBarWidget extends StatelessWidget {
  final bool showAppBar;
  final POISearchBaseConfig searchConfig;
  final POISearchBarViewModel viewModel;

  final VoidCallback clickCancel;
  final AirportCityChangeCallback airportCityChangeCallback;
  final ValueChanged<List<POISearchCellViewModel>> onFinishSelection;
  final ValueChanged<T3SearchCondition> searchConditionChanged;

  const SearchBarWidget({
    Key? key,
    required this.searchConfig,
    required this.showAppBar,
    required this.clickCancel,
    required this.viewModel,
    required this.airportCityChangeCallback,
    required this.onFinishSelection,
    required this.searchConditionChanged,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget inputBoard;
    var showBackButton = false;
    if (searchConfig is POISearchSinglePointConfig) {
      var singleConfig = searchConfig as POISearchSinglePointConfig;
      inputBoard = T3SingleInputBoard(
        viewModel: viewModel,
        searchConfig: singleConfig,
        onFinishSelection: onFinishSelection,
        onSearchConditionDidChanged: searchConditionChanged,
      );
      showBackButton = singleConfig.showBackButton;
    } else {
      assert(searchConfig is POISearchMultiPointsConfig, '必须是多点选择');
      var multiConfig = searchConfig as POISearchMultiPointsConfig;
      inputBoard = T3MultiInputBoard(
        viewModel: viewModel,
        searchConfig: multiConfig,
        airportCityChangeCallback: airportCityChangeCallback,
        onFinishSelection: onFinishSelection,
        onSearchConditionDidChanged: searchConditionChanged,
      );
    }
    return Container(
      padding: EdgeInsets.only(top: showAppBar ? 0 : MediaQuery.of(context).padding.top),
      decoration: const BoxDecoration(color: Colors.white),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (showBackButton)
            Container(
              width: InputConst.kBackButtonWidth,
              height: searchConfig.style.inputItemHeight(),
              margin: const EdgeInsets.only(top: 4),
              decoration: const BoxDecoration(color: Colors.white),
              child: IconButton(
                  onPressed: () {
                    logPOISearchDebug('搜索框点击 返回 按钮');
                    BoostNavigator.instance.pop();
                  },
                  icon:
                  T3Image.t3assets(T3AssetsName.icon_back_elderly_slices)),
            ),
          if (!showBackButton)
            const SizedBox(width: 12,),
          Expanded(child: inputBoard),
          const SizedBox(width: 12,),
          T3GestureDetector(
            onTap: () {
              logPOISearchDebug('搜索框点击 取消 按钮');
              clickCancel();
            },
            child: Container(
              height: searchConfig.style.inputItemHeight(),
              margin: const EdgeInsets.only(top: 4),
              child: const Center(
                child: Text(
                  '取消',
                  style: TextStyle(
                      color: LucyColors.lucy_747881,
                      fontSize: LucyFonts.size_14,
                      fontWeight: LucyFonts.regular
                  ),
                ),
              ),
            ),
          ),
          const SizedBox(width: 12,),
        ],
      ),
    );
  }
}
